package com.dxp.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.dxp.utils.XMLUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname SelectMsgFormatPluginImpl
 * @Description TODO
 * @Date 2020/3/26 19:07
 * @Created by fpc
 */
public class SelectMsgFormatPluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        ResultEntity result = new ResultEntity();
        Map<String,Object> resultMap = new HashMap<>();
        result.setResult("F");
        Object data = null;
        String msgFormat = String.valueOf(map.get("msgFormatType"));
        String msgType = String.valueOf(map.get("msgType"));
        if(Validator.isNull(msgType)){
            result.setResult("F");
            result.setEntity("参数【msgType】为空，请检查参数。");
            resultMap.put(CustomMethodPlugin.resultMark,"ok");
            resultMap.put(CustomMethodPlugin.pluginInnerReturn,result);
            return resultMap;
        }
        Map<String,Object> whereMap = new HashMap();
        whereMap.put("dxMsgFormatSet.w_interfaceId",map.get("interfaceId"));
        whereMap.put("dxMsgFormatSet.w_msgType",msgType);
        ResultEntity resultEntity = IusPluginUtil.getInstance().select("list", "s_dxMsgFormatSet", whereMap);
        Map<String, Object> menuList = new HashMap<>();
        if(!Validator.isEmpty(resultEntity.getEntity())){
            List<DataMap> dataMapList = (List<DataMap>)resultEntity.getEntity();

            for(DataMap<String> dataMap: dataMapList){
                if("0".equals(dataMap.get("paramParent"))){//通过判断，0代表没有父级，也就是一级
                    if("1".equals(dataMap.get("ifSubParam"))) {
                        menuList.put(dataMap.get("paramEname"), getChildren(dataMapList, dataMap.get("infoId"),dataMap.get("paramEname")));//继续往一级下面遍历，调用下面的处理方法，放入树状结构
                    }else {
                        menuList.put(dataMap.get("paramEname"),StrUtil.doNull(dataMap.get("defValue"),""));
                    }
//                    menuList.put(dataMap.get("paramEname"),newMap);//先把一级存入结果
                }
            }
            log.info("menuList---->>>>"+menuList.toString());
            if(null != menuList && !menuList.isEmpty()) {
                if ("xml".equals(msgFormat)) {
                    data = XMLUtil.callMapToXML(menuList);
                } else {
                    data = JSONObject.fromObject(menuList.get("defaultRoot"));
                }
                result.setResult("T");
                result.setEntity(data.toString());
                log.info("data---->>>"+data);
            }else{
                result.setEntity("");
            }
        }


        resultMap.put(CustomMethodPlugin.resultMark,"ok");
        resultMap.put(CustomMethodPlugin.pluginInnerReturn,result);
        return resultMap;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }

    //结果集转树状结构
    private Map<String, Object> getChildren(List<DataMap> data,String menuId,String parentEname){//参数为数据库的（原数据，一级id）
        if(data==null || data.size()==0 || StrUtil.isNull(menuId)){
            return null;
        }
        Map<String, Object> menuList = new HashMap<>();
        Map tempMap =null;
        for(DataMap<String> vo:data ){
            if(menuId.equals(vo.get("paramParent"))){//如果本级id与下面数据的父id相同，就说明是子父级关系
                if(menuList.containsKey(vo.get("paramEname"))){
                    List list = new ArrayList();
                    Object obj = menuList.get(vo.get("paramEname"));
                    if(Validator.isNull(String.valueOf(obj))){
                        tempMap = new HashMap();
                        tempMap.put(vo.get("paramEname"),StrUtil.doNull(vo.get("defValue"),""));
                        list.add(tempMap);
                    }else{
                        tempMap = new HashMap();
                        tempMap.put(vo.get("paramEname"),obj);
                        list.add(tempMap);
                    }
                    menuList.remove(vo.get("paramEname"));

                    if("1".equals(vo.get("ifSubParam"))) {
                        tempMap = new HashMap();
                        tempMap.put(vo.get("paramEname"),getChildren(data, vo.get("infoId"),vo.get("paramEname")));
                        list.add(tempMap);
                        menuList.put(parentEname, list);//递归，查询子级下的子级
                    }else {
                        tempMap = new HashMap();
                        tempMap.put(vo.get("paramEname"),StrUtil.doNull(vo.get("defValue"),""));
                        list.add(tempMap);
                        menuList.put(parentEname,list);
                    }
                }else{
                    if("1".equals(vo.get("ifSubParam"))) {
                        menuList.put(vo.get("paramEname"), getChildren(data, vo.get("infoId"),vo.get("paramEname")));//递归，查询子级下的子级
                    }else {
                        menuList.put(vo.get("paramEname"),StrUtil.doNull(vo.get("defValue"),""));
                    }
                }
            }
        }
        return menuList;
    }
}
